package com.link2room.soundwave.facade.impl;

import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.TryExceptionUtil;
import com.link2room.enums.exception.SoundwaveSysExceptionEnum;
import com.link2room.soundwave.dto.param.manager.L2RPMSCouponManagerParamDto;
import com.link2room.soundwave.dto.result.base.L2RPmsCouponDto;
import com.link2room.soundwave.entity.L2RPmsCoupon;
import com.link2room.soundwave.exception.SoundwaveException;
import com.link2room.soundwave.facade.IL2RPMSCouponFacadeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Optional;

import static com.link2room.common.enums.BaseSysExceptionEnum.PARAM_IS_NULL;

/**
 * @author sqj88
 * @date 2018/1/5 14:23
 */
@Component("l2RPMSCouponFacadeService")
public class L2RPMSCouponFacadeServiceImpl implements IL2RPMSCouponFacadeService {

    @Resource
    private ICommonService commonService;

    /**
     * 获取联房PMS电子券对照列表
     * @param l2RPMSCouponManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<L2RPmsCouponDto> listL2RPMSCoupons(L2RPMSCouponManagerParamDto l2RPMSCouponManagerParamDto)throws Exception{
        L2RPmsCoupon query=new L2RPmsCoupon();
        query.setLhotelGroupCode(l2RPMSCouponManagerParamDto.getLhotelGroupCode());
        query.setL2RCouponType(l2RPMSCouponManagerParamDto.getL2RCouponType());
        QueryResult<L2RPmsCoupon> l2RPmsCouponQueryResult = commonService.findAllEQ(L2RPmsCoupon.class, query);
        return ClassConverUtil.copyQueryResultToDtoQueryResult(l2RPmsCouponQueryResult,L2RPmsCouponDto.class,false);
    }

    /**
     * 检查PMS电子券代码
     * @param l2RPMSCouponManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public void checkPmsCouponCode(L2RPMSCouponManagerParamDto l2RPMSCouponManagerParamDto)throws Exception{
        L2RPmsCoupon query=new L2RPmsCoupon();
        query.setLhotelGroupCode(l2RPMSCouponManagerParamDto.getLhotelGroupCode());
        query.setPmsCouponCode(l2RPMSCouponManagerParamDto.getPmsCouponCode());
        QueryResult<L2RPmsCoupon> l2RPmsCouponQueryResult = commonService.findAllEQ(L2RPmsCoupon.class, query);
       if(l2RPmsCouponQueryResult.getResultlist()!=null&&l2RPmsCouponQueryResult.getResultlist().size()>0){
           throw new SoundwaveException(SoundwaveSysExceptionEnum.COUPON_MAPPING_IS_EXIT);
       }
    }

    /**
     * 获取联房PMS电子券对照详情
     * @param baseL2rParamDto
     * @return
     * @throws Exception
     */
    @Override
    public L2RPmsCouponDto getL2RPMSCoupon(BaseL2rParamDto baseL2rParamDto)throws Exception{
        L2RPmsCoupon l2RPmsCoupon= Optional.ofNullable(baseL2rParamDto.getId()).map(
                TryExceptionUtil.of(value->{return commonService.findOneById(L2RPmsCoupon.class,value);})
        ).orElseThrow(()->new SoundwaveException(PARAM_IS_NULL,"id"));
        return ClassConverUtil.copyPropertiesToDto(l2RPmsCoupon,L2RPmsCouponDto.class,false);
    }

    /**
     * 保存联房PMS电子券对照
     * @param l2RPMSCouponManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public L2RPmsCouponDto saveL2RPMSCoupon(L2RPMSCouponManagerParamDto l2RPMSCouponManagerParamDto)throws Exception{
        return Optional.ofNullable(l2RPMSCouponManagerParamDto.getL2RPmsCouponDto()).map(
                TryExceptionUtil.of(l2RPmsCouponDto->{
                    L2RPmsCoupon l2RPmsCoupon=ClassConverUtil.copyProperties(l2RPmsCouponDto,L2RPmsCoupon.class);
                    commonService.save(l2RPmsCoupon);
                    l2RPmsCouponDto.setId(l2RPmsCoupon.getId());
                    return l2RPmsCouponDto;
                })
        ).orElseThrow(()->new SoundwaveException(BaseSysExceptionEnum.PARAM_IS_NULL,"l2RPmsCouponDto"));
    }

    /**
     * 更新联房PMS电子券对照
     * @param l2RPMSCouponManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public L2RPmsCouponDto updateL2RPMSCoupon(L2RPMSCouponManagerParamDto l2RPMSCouponManagerParamDto)throws Exception{
        return Optional.ofNullable(l2RPMSCouponManagerParamDto.getL2RPmsCouponDto()).map(
                TryExceptionUtil.of(l2RPmsCouponDto->{
                    L2RPmsCoupon l2RPmsCoupon=ClassConverUtil.copyProperties(l2RPmsCouponDto,L2RPmsCoupon.class);
                    commonService.merge(l2RPmsCoupon);
                    l2RPmsCouponDto.setId(l2RPmsCoupon.getId());
                    return l2RPmsCouponDto;
                })
        ).orElseThrow(()->new SoundwaveException(BaseSysExceptionEnum.PARAM_IS_NULL,"l2RPmsCouponDto"));
    }
}
